package com.dididoctor.patient.Service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

import com.dididoctor.patient.Activity.Service.ServiceDetail.ServiceDetailActivity;
import com.dididoctor.patient.R;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.model.CustomNotification;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

/**
 * Created by liying on 2016/5/2.
 */
public class NatificationService extends Service {
    private PendingIntent intents;
    private static final Random random = new Random(System.currentTimeMillis());
    private NotificationManager manager;
    private Notification notification;

    private Vibrator mVibrator01;  //声明一个振动器对象
    private BroadcastReceiver mBR;
    private IntentFilter mIF;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        mBR = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                Intent a = new Intent(NatificationService.this, NatificationService.class);
                startService(a);
            }
        };
        mIF = new IntentFilter();
        mIF.addAction("listener");
        registerReceiver(mBR, mIF);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(new Observer<CustomNotification>() {
            @Override
            public void onEvent(CustomNotification message) {
//                    "app": "1",
//                            "webUrl": "https://www.baidu.com/",
//                            "picUrl": "http://192.168.1.181:8080/doctor/upload/bankLogo/1.png",
//                            "type": "1", 活动是1诊断书是2咨询过期是3
//                            "msg": "在线咨询7.5折"
                String content = message.getContent();
                Log.e("CONTENT", content);
                String des = "";
                String webUrl = "";
                String type = "";
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    des = jsonObject.getString("msg");
                    type = jsonObject.getString("type");
                    webUrl = jsonObject.getString("webUrl");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                //点击通知栏跳转的界面设置
                if (type.equals("1")) {
                    Intent intent1 = null;
                    intent1 = new Intent(getApplication(), ServiceDetailActivity.class);
                    intent1.putExtra("webUrl", webUrl);
                    Log.e("WEB_URL", webUrl);
//                        intent1.putExtra("orderID", chatMessage.getOrderID());
//                        intent1.putExtra("coursesId", chatMessage.getCourseID());
//                        intent1.putExtra("studentID", chatMessage.getSendUserID());
//                        intent1.putExtra("studentName", chatMessage.getSendUserName());
                    //延迟意图设置进入通知栏用
                    intents = PendingIntent.getActivity(getApplication(), random.nextInt(),
                            intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                }
                notification = new Notification();
                notification.icon = R.drawable.applogos; // 显示的图标
                notification.defaults = Notification.DEFAULT_SOUND;
                notification.defaults |= Notification.DEFAULT_LIGHTS;
                notification.defaults |= Notification.DEFAULT_VIBRATE;
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                notification.when = System.currentTimeMillis();

                //设置标题  和内容
                String title = "滴滴医生";


                /** 设置通知消息 */
                //需要标题和内容   title通知的标题  des通知的内容
                notification.setLatestEventInfo(getApplication(), title, des, intents);

                manager.notify(0, notification);
                //消息振动器
                mVibrator01 = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
                mVibrator01.vibrate(new long[]{100, 10, 100, 1000}, -1);
            }
        }, true);

        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Intent localIntent = new Intent();
        localIntent.setClass(this, NatificationService.class);  //销毁时重新启动Service
        this.startService(localIntent);
        super.onDestroy();
    }
}
